package ch.ti.bfh.app.loodel.web.utils;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

public class JodaDateTimeConverter implements Converter {

    private String pattern = "yyyy-MM-dd HH:mm";

    @Override
    public Object getAsObject(FacesContext ctx, UIComponent component, String value) {
        return DateTimeFormat.forPattern(pattern).withLocale(ctx.getViewRoot().getLocale()).parseDateTime(value);
    }

    @Override
    public String getAsString(FacesContext ctx, UIComponent component, Object value) {
        DateTime dateTime = (DateTime) value;
        return DateTimeFormat.forPattern(pattern).withLocale(ctx.getViewRoot().getLocale()).print(dateTime);
    }


    public String getPattern() {
        return pattern;
    }

    public void setPattern(String pattern) {
        this.pattern = pattern;
    }
}
